<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>  
<html>  
    <head>  
    <meta charset="utf-8">  
    <title>映射关系定义</title>
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/default/easyui.css" />
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/icon.css" />
	    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/ainit.css" type="text/css"></link>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
   		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.1.2.2.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
	 	<script type="text/javascript">
			var pageNumber;
			var pageSize;
			var a = document.documentElement.clientHeight;
			//源数据分组    //目标数据分组
			var excGroups = new Array();
			//运行模式
			var runMod = new Array();
			//变量
			var mapGroupId;
			var mapTableId;
			
			$(function(){
				mapGroupId = "";
			    mapTableId = "";
			
				$('input').attr('class','ainit-input');
				$('input[type=checkbox]').removeAttr('class');
				pageNumber = 1;
				pageSize = 15;
				a = document.documentElement.clientHeight - 20;
				
				//添加窗口
				$('#kk').window({  
								title:'交换组定义',
							    width:600,  
							    height:400,  
							    modal:true  
							});  
				$('#kk').window('close');  //关闭窗口 
				
				loadSourceInfo();
				$('#mgt').window({  
	 							title:'交换项定义',
							    width:document.documentElement.clientWidth,  
							    height:document.documentElement.clientHeight,  
							    draggable:false,
							    modal:true,
							    resizable:false
							});  
				$('#mgt').window('close');  //关闭窗口 
				
				$('#mgc').window({  
	 							title:'映射字段定义',
							    width:document.documentElement.clientWidth,  
							    height:document.documentElement.clientHeight,  
							    draggable:false,
							    modal:true,
							    resizable:false
							});  
				$('#mgc').window('close');  //关闭窗口 
				
			});
			
			function loadSourceInfo(){
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/loadMappingGroupDz',
					type:'post',
					data:{},
					success:function(data){
						for(var i = 0; i < data[0].length; i++){
							var excGroup = {};
							excGroup.ID = data[0][i].ID;
							excGroup.NAME = data[0][i].NAME;
							excGroups[i] = excGroup;
							$('#groupForm select[name=sourceGroupId]').append("<option value=\'"+ data[0][i].ID +"\'>"+data[0][i].NAME+"</option>");
							$('#groupForm select[name=targetGroupId]').append("<option value=\'"+ data[0][i].ID +"\'>"+data[0][i].NAME+"</option>");
						}
						for(var i = 0; i < data[1].length; i++){
							var runM = {};
							runM.ID = data[1][i].ID;
							runM.NAME = data[1][i].NAME;
							runMod[i] = runM;
							$('#groupForm select[name=runMode]').append("<option value=\'"+ data[1][i].ID +"\'>"+data[1][i].NAME+"</option>");
						}
						loadData();
						loadSourceInfoD();
					}
				});
			}
			
			
			function excGroupFormatter(value){
				for(var i=0; i<excGroups.length; i++){
					if (excGroups[i].ID == value) return excGroups[i].NAME;
				}
				return '';
			}
			
			function runModFormatter(value){
				for(var i=0; i<runMod.length; i++){
					if (runMod[i].ID == value) return runMod[i].NAME;
				}
				return '';
			}
			
			function isVerifyFormatter(value){
				if(value=='T'){
					return '启用';
				}else{
					return '';
				}
			}
			
			function isVerStyFormatter(value){
				if(value=='T'){
					return 'background:#66cc33';
				}
			}
			
			function loadData(){
				var lastIndex = -1;
				var addRow = -1;
				$('#tt').datagrid({  
				    url:'${pageContext.request.contextPath}/edi/queryMappingGroup',  
				    width:function(){return document.documentElement.clientWidth -30},
					height:a,
					pageList:[20,30,50,100],
				    iconCls:'icon-save',
					nowrap: false,
					striped: true,				 
					collapsible:true,
					autoRowHeight: false,		
					sortName: 'GROUP_MAPPING_NAME',
					sortOrder: 'desc',
					remoteSort: false,
					idField:'code',
					fitColumns:true,
					singleSelect:true,
				    columns:[[  
				        {field:'GROUP_MAPPING_ID',title:'GROUP_MAPPING_ID',width:100,hidden:true},  
				        {field:'GROUP_MAPPING_NAME',title:'映射名',width:100},  
				        {field:'SOURCE_GROUP_ID',title:'源分组',width:100,align:'center',
				        	 formatter:excGroupFormatter,
				        	editor:{type:'combobox',options:{valueField:'ID',textField:'NAME',data:excGroups}}},  
				        {field:'TARGET_GROUP_ID',title:'目标分组',width:100,align:'center',
				        	formatter:excGroupFormatter,
				        	editor:{type:'combobox',options:{valueField:'ID',textField:'NAME',data:excGroups}}},  
				        {field:'RUN_MODE',title:'运行模式',width:100,align:'center',
				        	formatter:runModFormatter,
				        	editor:{type:'combobox',options:{valueField:'ID',textField:'NAME',data:runMod}}},  
				        {field:'RUN_TIME_PERIOD',title:'自动交换时间间隔',width:100,align:'center' },  
				        {field:'IS_VALID',title:'是否启用',width:100,align:'center',formatter:isVerifyFormatter,styler:isVerStyFormatter},
				        {field:'LAST_RUN_TIME',title:'最后一次运行时间',width:100,align:'center' },
				        {field:'RUN_ORDER',title:'执行次序',width:100,align:'center' },
				        {field:'MEMO',title:'备注',width:100,align:'center'}  
				    ]],
					pagination:true,
					rownumbers:true,
					toolbar:[{
						id:'btnadd',
						text:'添加',
						iconCls:'icon-add',
						handler:function(){
							loadMapTable();
							resetForm();
							$('#mgt').window('open');  //打开窗口 
						}					
					},{
						id:'btncut',
						text:'删除',
						iconCls:'icon-cut',
						handler:function(){
							alert('cut')
						}
					}],
					onBeforeLoad:function(){
						$('#tt').datagrid('rejectChanges');
					},
					onDblClickRow:function(rowIndex, rowData){
						mapGroupId = rowData.GROUP_MAPPING_ID;
						fillMapTableInfo(rowData);
						loadMapTable();
						$('#mgt').window('open');  //打开窗口 
						//填充
					}
				});
			}
			
			function addMapGroup(){
				if(checkForm()){
					return;
				}
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/addMapGroup',
					type:'post',
					data:$('#groupForm').serialize(),
					success:function(data){
						if(data.msg == 'upsuccess'){
							$.messager.alert('提示',"更新成功");
							$('#tt').datagrid('reload');
						}else if(data.msg == 'success'){
							$.messager.alert('提示',"保存成功");
							$('#groupForm input[name=groupMappingId]').val(data.info);
							$('#tt').datagrid('reload');
						}else{
							$.messager.alert('提示',data.msg);
						}
					}
				});
			}
			
			function checkForm(){
				if($('#groupForm input[name=groupMappingName]').val()==''){
					$.messager.alert('提示',"请填入映射组名称","warning");
					$('#groupForm input[name=groupMappingName]').focus();
					return true;
				}
				if($('#groupForm select[name=sourceGroupId]').val()=='00'){
					$.messager.alert('提示',"请选择源交换项","warning");
					return true;
				}
				if($('#groupForm select[name=targetGroupId]').val()=='00'){
					$.messager.alert('提示',"请选择目标交换项","warning");
					return true;
				}
				return false;
			}
			
			function resetForm(){
				$('#groupForm input[name=groupMappingName]').val('');
				$('#groupForm input[name=runTimePeriod]').val('');
				$('#groupForm input[name=runOrder]').val('');	
				$('#groupForm input[name=memo]').val('');
			}
/*****交换组结束**********************************************************************/
			
/*****交换项开始*******************************************************************/
			function fillMapTableInfo(rowData){
				$('#groupForm input[name=groupMappingName]').val(rowData.GROUP_MAPPING_NAME);
				$('#groupForm input[name=groupMappingId]').val(rowData.GROUP_MAPPING_ID);
				$('#groupForm select[name=sourceGroupId]').val(rowData.SOURCE_GROUP_ID); 
				$('#groupForm select[name=targetGroupId]').val(rowData.TARGET_GROUP_ID); 
				$('#groupForm select[name=runMode]').val(rowData.RUN_MODE);
				$('#groupForm input[name=runTimePeriod]').val(rowData.RUN_TIME_PERIOD);
				if(rowData.IS_VALID == 'T'){
					$('#groupForm input[name=isValid]').attr("checked", "checked");
				}else{
					$('#groupForm input[name=isValid]').removeAttr("checked");
				}
				$('#groupForm input[name=memo]').val(rowData.MEMO);
			}
			
			function loadMapTable(){
				var lastIndex = -1;
				var addRow = -1;
				$('#mgtList').datagrid({  
				    url:"${pageContext.request.contextPath}/edi/queryMappingTable?mappingGroupId="+mapGroupId,  
					pageList:[20,30,50,100],
				    iconCls:'icon-save',
					nowrap: false,
					striped: true,				 
					collapsible:true,
					autoRowHeight: false,		
					sortName: 'GROUP_MAPPING_NAME',
					sortOrder: 'desc',
					remoteSort: false,
					idField:'code',
					fitColumns:true,
					singleSelect:true,
				    columns:[[  
				        {field:'TABLE_MAPPING_ID',title:'GROUP_MAPPING_ID',width:100,hidden:true},  
				        {field:'TABLE_MAPPING_NAME',title:'映射名',width:100},  
				        {field:'SOURCE_TABLE_ID',title:'源交换项',width:100,align:'center',
				        	 formatter:excGroupFormatter,
				        	editor:{type:'combobox',options:{valueField:'ID',textField:'NAME',data:excGroups}}},  
				        {field:'TARGET_TABLE_ID',title:'目标交换项',width:100,align:'center',
				        	formatter:excGroupFormatter,
				        	editor:{type:'combobox',options:{valueField:'ID',textField:'NAME',data:excGroups}}},  
				        {field:'TABLE_CONVERT_MODE',title:'转换模式',width:100,align:'center',
				        	formatter:runModFormatter,
				        	editor:{type:'combobox',options:{valueField:'ID',textField:'NAME',data:runMod}}},  
				        {field:'TABLE_CONVERT_CLASS',title:'转换类',width:100,align:'center' },  
				        {field:'IS_DIVIDE_PAGE',title:'是否分页',width:100,align:'center',formatter:isVerifyFormatter,styler:isVerStyFormatter},
				        {field:'PAGE_ROWS',title:'分页行数',width:100,align:'center' },
				        {field:'LAST_EXCHANGE_DATE',title:'最后执行时间',width:100,align:'center' },
				        {field:'INSERT_ROWS',title:'插入行数',width:100,align:'center'},  
				        {field:'UPDATE_ROWS',title:'更新行数',width:100,align:'center'},  
				        {field:'DELETE_ROWS',title:'删除行数',width:100,align:'center'},
				        {field:'ERROR_ROWS',title:'错误行数',width:100,align:'center'},
				        {field:'RUN_ORDER',title:'运行次序',width:100,align:'center'},
				        {field:'MEMO',title:'备注',width:100,align:'center'}  
				    ]],
					pagination:true,
					rownumbers:true,
					toolbar:[{
						id:'btnadd',
						text:'添加',
						iconCls:'icon-add',
						handler:function(){
							$('#mgc').window('open');  //打开窗口 
							resetTForm();
						}
					},{
						id:'btncut',
						text:'删除',
						iconCls:'icon-cut',
						handler:function(){
							alert('cut')
						}
					}],
					onBeforeLoad:function(){
						$('#tt').datagrid('rejectChanges');
					},
					onDblClickRow:function(rowIndex, rowData){
						fillMapTable(rowData);
						$('#mgc').window('open');  //打开窗口 
					}
				});
			}
			//填充交换项
			function fillMapTable(mapTable){
				$('#tableForm input[name=tableMappingName]').val(mapTable.TABLE_MAPPING_NAME);
				$('#tableForm input[name=tableMappingId]').val(mapTable.TABLE_MAPPING_ID);
				$('#tableForm select[name=sourceTableId]').val(mapTable.SOURCE_TABLE_ID);
				$('#tableForm select[name=targetTableId]').val(mapTable.TARGET_TABLE_ID);
				$('#tableForm select[name=tableConvertMode]').val(mapTable.TABLE_CONVERT_MODE);
				$('#tableForm input[name=tableConvertClass]').val(mapTable.TABLE_CONVERT_CLASS);
				if(mapTable.IS_DIVIDE_PAGE == 'T'){
					$('#tableForm input[name=isDividePage]').attr("checked", "checked");
				}else{
					$('#groupForm input[name=isDividePage]').removeAttr("checked");
				}
				$('#tableForm input[name=lastExchangeDate]').val(mapTable.LAST_EXCHANGE_DATE);
				$('#tableForm input[name=insertRows]').val(mapTable.INSERT_ROWS);
				$('#tableForm input[name=updateRows]').val(mapTable.UPDATE_ROWS);
				$('#tableForm input[name=deleteRows]').val(mapTable.DELETE_ROWS);
				$('#tableForm input[name=errorRows]').val(mapTable.ERROR_ROWS);
				$('#tableForm input[name=runOrder]').val(mapTable.RUN_ORDER);
				$('#tableForm input[name=memo]').val(mapTable.MEMO);
			}
			
			function resetTForm(){
				$('#tableForm input[name=tableMappingName]').val('');
				$('#tableForm input[name=tableMappingId]').val('');
			}
			
			function loadSourceInfoD(){
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/loadMappingTableDz',
					type:'post',
					data:{},
					success:function(data){
						var rtrees = new Array();
						for(var i = 0; i < excGroups.length; i++){
							var tree = {};
							tree.id = excGroups[i].ID;
							tree.text = excGroups[i].NAME;
							tree.state = 'closed';
							tree.children = formatRootExcTable(excGroups[i].ID,data[0],data[1]);
							rtrees[i] = tree;
						}
						$('#sourceTableId').combotree({
		 					animate:true,
							data: rtrees
						});	
						$('#targetTableId').combotree({
		 					animate:true,
							data: rtrees
						});	
					}
				});
			}
			//解析root
			function formatRootExcTable(groupId,rootExcTabArr,chilExcTabArr){
				var ctrees = new Array();
				var c = 0;
				for(var i = 0; i < rootExcTabArr.length; i++){
					if(rootExcTabArr[i].GROUP_ID == groupId){
						var tree = {};
						tree.id = rootExcTabArr[i].GROUP_TABLE_ID;
						tree.text = rootExcTabArr[i].TABLE_NAME;
						tree.children = formatChildren(rootExcTabArr[i].GROUP_TABLE_ID,chilExcTabArr);
						ctrees[c++] = tree;
					}
				}
				return ctrees;
			}
			//解析children
			function  formatChildren(groupTableId,list){
	 			var tmpTreeArr = new Array();
	 			var chiL = 0;
	 			for (var i = 0; i < list.length; i++){
	 				if(list[i].SUPER_TABLE_ID == groupTableId){
		 				var tmpTree = {};
		 				tmpTree.id = list[i].GROUP_TABLE_ID;
		 				tmpTree.text = list[i].TABLE_NAME;
		 				tmpTree.children = formatChildren(list[i].GROUP_TABLE_ID,list);
		 				//if(tmpTree.children != ''){
			 				//tmpTree.state = "closed"; 
		 				//}
		 				list.remove(i);
		 				tmpTreeArr[chiL++] = tmpTree;
	 				}
	 			}
	 			return tmpTreeArr;
	 		}
	 		//添加交换项
	 		function addMapTable(){
	 			if(checkForm()){
					return;
				}
				$.ajax({
					url:'${pageContext.request.contextPath}/edi/addMapTable',
					type:'post',
					data:$('#tableForm').serialize()+'&groupMappingId='+mapGroupId,
					success:function(data){
						if(data.msg == 'upsuccess'){
							$.messager.alert('提示',"更新成功");
							$('#mgtList').datagrid('reload');
						}else if(data.msg == 'success'){
							$.messager.alert('提示',"保存成功");
							$('#tableForm input[name=tableMappingId]').val(data.info);
							$('#mgtList').datagrid('reload');
						}else{
							$.messager.alert('提示',data.msg);
						}
					}
				});
	 		}
	 	</script>
    </head>  
    <body>  
    	<!-- 数据源查询界面 -->
    	<table id="tt"></table>  
    
    	<div id='mgt' style="width:100%;height:100%;">
    		<div id="tt" class="easyui-tabs" fit='true' border='false' >  
			    <div title="交换组" style="padding:5px;">  
			        <!-- 数据源配置界面 -->
			    	<form id="groupForm">
				    	<table>
				    		<tr>
				    			<td>映射名称:</td>
				    			<td colspan="3" style="width:465px;">
				    				<input name='groupMappingName' style="width:460px;"/><input name='groupMappingId' style="display: none;"/></td>
				    		</tr>
				    		<tr>
				    			<td>源分组:</td>
				    			<td style="width:175px;">
				    				<select  name='sourceGroupId' style="width: 100%">
				    					<option value='00'>==请选择==</option>
				    				</select>
				    			</td>
				    			<td>目标分组:</td>
				    			<td>
				    				<select name='targetGroupId'>
				    					<option value='00'>==请选择==</option>
				    				</select>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td>启动模式:</td>
				    			<td>
									<select name='runMode'>
				    					<option value='00'>==请选择==</option>
				    				</select>
								</td>
				    			<td>自动交换间隔(min):</td>
				    			<td><input name='runTimePeriod' /></td>
				    		</tr>
				    		<tr>
				    			<td>是否启用:</td>
				    			<td align="left"><input name='isValid' type="checkbox" /></td>
				    			<td>执行次序:</td>
				    			<td><input name='runOrder' /></td>
				    		</tr>
				    		<tr>
				    			<td>备注:</td>
				    			<td colspan="3"><textarea name='memo' cols="55" rows="3"></textarea></td>
				    		</tr>
				    		<tr>
				    			<td></td>
				    			<td colspan="3">
				    				<a href="#" class="easyui-linkbutton" onclick="javascript:addMapGroup()">保存</a> 
				    				<a href="#" class="easyui-linkbutton" onclick="javascript:resetForm()">添加</a> 
				    			</td>
				    		</tr>
				    	</table>
			    	</form>
			    </div>  
			    <div title="交换项" style="padding:5px;">  
			        <div id='mgtList'></div>
			    </div>  
			</div>  
    	</div>
    	<div id='mgc' style="width:100%;height:100%;">
    		<div id="mm" class="easyui-tabs" fit='true' border='false' >  
			    <div title="交换项信息" style="padding:5px;">  
			        <!-- 数据源配置界面 -->
			    	<form id="tableForm">
				    	<table>
				    		<tr>
				    			<td>映射项名称:</td>
				    			<td colspan="3" style="width:465px;">
				    			<input name='tableMappingName' style="width:460px;"/><input name='tableMappingId' style="display: none;"/></td>
				    		</tr>
				    		<tr>
				    			<td>源交换项:</td>
				    			<td style="width:175px;">
				    				<select id='sourceTableId' style="width:160px;"  name='sourceTableId'>
				    				</select>
				    			</td>
				    			<td>目标交换项:</td>
				    			<td>
				    				<select id='targetTableId' style="width:160px;"  name='targetTableId'>
				    				</select>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td>转换方式:</td>
				    			<td>
									<select name='tableConvertMode'>
				    					<option value='0'></option>
				    					<option value='1'>自动</option>
				    					<option value='2'>定制类</option>
				    				</select>
								</td>
				    			<td>转换类:</td>
				    			<td><input name='tableConvertClass' /></td>
				    		</tr>
				    		<tr>
				    			<td>是否分页:</td>
				    			<td align="left"><input name='isDividePage' type="checkbox" /></td>
				    			<td>每页行数:</td>
				    			<td><input name='pageRows' /></td>
				    		</tr>
				    		<tr>
				    			<td>备注:</td>
				    			<td colspan="3"><textarea name='memo' cols="55" rows="3"></textarea></td>
				    		</tr>
				    		<tr>
				    			<td>最后执行时间:</td>
				    			<td align="left"><input disabled="disabled" name='lastExchangeDate' /></td>
				    			<td>新增行数:</td>
				    			<td><input disabled="disabled"  name='insertRows' /></td>
				    		</tr>
				    		<tr>
				    			<td>更行行数:</td>
				    			<td align="left"><input disabled="disabled"   name='updateRows'/></td>
				    			<td>删除行数:</td>
				    			<td><input disabled="disabled"  name='deleteRows' /></td>
				    		</tr>
				    		<tr>
				    			<td>错误行数:</td>
				    			<td align="left"><input disabled="disabled"  name='errorRows'/></td>
				    			<td>执行次序:</td>
				    			<td><input disabled="disabled" name='runOrder' /></td>
				    		</tr>
				    		<tr>
				    			<td></td>
				    			<td colspan="3">
				    				<a href="#" class="easyui-linkbutton" onclick="javascript:addMapTable()">保存</a> 
				    				<a href="#" class="easyui-linkbutton" onclick="javascript:resetTForm()">添加</a> 
				    			</td>
				    		</tr>
				    	</table>
			    	</form>
			    </div>  
			    <div title="交换字段映射" style="padding:5px;"> 
			    	
			    </div>  
			</div>  
    	</div>
	</body>  
</html>  